
var currentTime = "";


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// init

function initMensa(){
	
	
	currentTime = new Date();
	
	$('#mensa_content').empty();
	
	buildMensaPlan(currentTime.getDate(), currentTime.getMonth(), currentTime.getFullYear());
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Onlick

$( document ).delegate("#mensa_menue_context_day_yesterday_button", "click", function() {
	
	$('#mensa_content').empty();
	
	currentTime.setDate(currentTime.getDate()-1);
	
	buildMensaPlan(currentTime.getDate(), currentTime.getMonth(), currentTime.getFullYear());
	
});


$( document ).delegate("#mensa_menue_context_day_tomorrow_button", "click", function() {

	$('#mensa_content').empty();
	
	currentTime.setDate(currentTime.getDate()+1);
	
	buildMensaPlan(currentTime.getDate(), currentTime.getMonth(), currentTime.getFullYear());
	
});




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions

/*	Build the full content of the Mensa
 * 
 * @param day, month, 
 * wenn du den Mensaplan eines bestimmten Tages haben moechtest, gib eine Zahl f�r den jeweiligen Parameter ein.
 * 
 */
function buildMensaPlan(day, month, year){
	
	var HTMLPlanAsString = "";
	
	var dayPrefix = getDayPrefix(0);
	var date = "";
	var dayNumber = getDayNumber(0);
	
	
	if(day != "" && day != null){
		
		date = day + "." + month + "." + year;
		
	}else{
		
		date = currentTime.getDate() + "." + currentTime.getMonth() + "." + currentTime.getFullYear();
	}
	
	
	
	HTMLPlanAsString = '<div id="mensa_menue_context" class="mensa_menue">'+
	
			'<div class="ui-grid-b">'+
				'<div class="ui-block-a">'+
						'<div id="mensa_menue_context_day_yesterday">'+
							'<a id="mensa_menue_context_day_yesterday_button" href="#" data-role="button" data-icon="arrow-l" data-theme="d">'+getDayPrefix(-1)+'</a>'+
						'</div>'+
					'</div>'+
					'<div class="ui-block-b">'+
						'<div id="mensa_menue_context_day_today">'+
							'<div id="mensa_menue_context_day_today_header">'+ dayPrefix +'</div>'+
							'<div id="mensa_menue_context_day_today_date">'+ date +'</div>'+
						'</div>'+
					'</div>'+
					'<div class="ui-block-c">'+
						'<div id="mensa_menue_context_day_tomorrow">'+
							'<a id="mensa_menue_context_day_tomorrow_button" href="#" data-role="button" data-icon="arrow-r" data-iconpos="right" data-theme="d">'+getDayPrefix(1)+'</a>'+
						'</div>'+
					'</div>'+
				'</div><!-- /grid-a -->'+
				
			'</div>';

	if(dayPrefix == "Sa" || dayPrefix == "So"){
		
		
		HTMLPlanAsString = HTMLPlanAsString + 'Heute gibt es nichts!';
		
	}else{
		
	
		for(var i = 0; i < 5; i++){
			HTMLPlanAsString = HTMLPlanAsString + 
				
				'<div id="mensa_menue_'+ i +'" class="mensa_menue">'+
				'<div class="mensa_menue_header_x">'+ jsonArrayOfTheMenuePlans[dayNumber].menues[i].name +'</div>'+
				'<div class="mensa_menue_content_x">'+ jsonArrayOfTheMenuePlans[dayNumber].menues[i].menue +'</div>'+
				'<div class="mensa_menue_price_x">'+ jsonArrayOfTheMenuePlans[dayNumber].menues[i].price +' &euro;</div>'+
				'</div>';
		}

							
	}

	$('#mensa_content').append(HTMLPlanAsString).trigger("create");
	
}

/*
 * 
 * @param isYesterday
 * Wenn du den Prefix von gestern haben m�chtest, dann gib den Wert -1, f�r morgen den Wert 1
 * und f�r heute alles m�gliche
 */
function getDayPrefix(whichDay){
	
	
	var Wochentag = new Array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");
	
	var anotherDate = new Date(currentTime.getDay() + "/" + currentTime.getMonth() + "/" + currentTime.getFullYear());
	
	var result = "";

	
	switch(whichDay){
	
		case (-1):
			
			currentTime.setDate(currentTime.getDate()-1);
		
			result = Wochentag[currentTime.getDay()];
			
			currentTime.setDate(currentTime.getDate()+1);
			
			break;
			
		case (1):
			
			currentTime.setDate(currentTime.getDate()+1);
		
			result = Wochentag[currentTime.getDay()];
		
			currentTime.setDate(currentTime.getDate()-1);
			
			break;
			
		default:
			
			result = Wochentag[currentTime.getDay()];
			
			break;
	}
	
	
	return result;
}

/*
 * 
 * @param isYesterday
 * Wenn du den Prefix von gestern haben m�chtest, dann gib den Wert -1, f�r morgen den Wert 1
 * und f�r heute alles m�gliche
 */
function getDayNumber(isYesterday){
	
	switch(getDayPrefix(isYesterday)){
	
		case "Mo":
			return 0;
			break;
		case "Di":
			return 1;
			break;
		case "Mi":
			return 2;
			break;
		case "Do":
			return 3;
			break;
		case "Fr":
			return 4;
			break;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// json's

var jsonArrayOfTheMenuePlans = [
                    
    {
    	"day":"Mo",
    	"menues": [
    	           	{"name":"Men&uuml; 1", "price":3.70, "menue":"Chicken Wings und Chicken Strips mit Joghurt Dip zus&auml;tzlich Fr&uuml;hlingsrollen als vegetarische Alternative" },
    	           	{"name":"Men&uuml; 2", "price":2.25, "menue":"H&ouml;llische Oberl&auml;nder Bratwurst mit Curry-Spezialsauce"},
    	           	{"name":"Wok", "price":2.45, "menue":"Paniertes Merlanfilet mit Kr&auml;uter-Dip und Zitrone"},
    	           	{"name":"Aktionstheke", "price":0.55, "menue":"Klare Rinderbr&uuml;he mit Nudeln"},
    	           	{"name":"Dessert", "price":0.30, "menue":"Vanillepudding"}
    	           ]	
    },
    {
    	"day":"Di",
    	"menues": [
	   	           	{"name":"Men&uuml; 1", "price":3.45, "menue":"Schweineschnitzel Wiener Art" },
    	           	{"name":"Men&uuml; 2", "price":2.75, "menue":"Hamburger (R) mit Eisbergsalat, Tomaten, Zwiebeln, Gurken und Sauce"},
    	           	{"name":"Wok", "price":2.30, "menue":"Hamburger (R) mit Eisbergsalat, Tomaten, Zwiebeln, Gurken und Sauce"},
    	           	{"name":"Aktionstheke", "price":0.65, "menue":"Gebrannte Grie&szlig;suppe"},
    	           	{"name":"Dessert", "price":0.30, "menue":"Joghurt, natur"}
    	           ]	
    },
    {
    	"day":"Mi",
    	"menues": [
	   	           	{"name":"Men&uuml; 1", "price":3.70, "menue":"Chicken Wings und Chicken Strips mit Joghurt Dip zus&auml;tzlich Fr&uuml;hlingsrollen als vegetarische Alternative" },
    	           	{"name":"Men&uuml; 2", "price":2.25, "menue":"H&ouml;llische Oberl&auml;nder Bratwurst mit Curry-Spezialsauce"},
    	           	{"name":"Wok", "price":2.45, "menue":"Paniertes Merlanfilet mit Kr&auml;uter-Dip und Zitrone"},
    	           	{"name":"Aktionstheke", "price":0.55, "menue":"Klare Rinderbr&uuml;he mit Nudeln"},
    	           	{"name":"Dessert", "price":0.30, "menue":"Vanillepudding"}
    	           ]	
    },
    {
    	"day":"Do",
    	"menues": [
	   	           	{"name":"Men&uuml; 1", "price":3.45, "menue":"Schweineschnitzel Wiener Art" },
    	           	{"name":"Men&uuml; 2", "price":2.75, "menue":"Hamburger (R) mit Eisbergsalat, Tomaten, Zwiebeln, Gurken und Sauce"},
    	           	{"name":"Wok", "price":2.30, "menue":"Hamburger (R) mit Eisbergsalat, Tomaten, Zwiebeln, Gurken und Sauce"},
    	           	{"name":"Aktionstheke", "price":0.65, "menue":"Gebrannte Grie&szlig;suppe"},
    	           	{"name":"Dessert", "price":0.30, "menue":"Joghurt, natur"}
    	           ]	
    },
    {
    	"day":"Fr",
    	"menues": [
	   	           	{"name":"Men&uuml; 1", "price":3.70, "menue":"Chicken Wings und Chicken Strips mit Joghurt Dip zus&auml;tzlich Fr&uuml;hlingsrollen als vegetarische Alternative" },
    	           	{"name":"Men&uuml; 2", "price":2.25, "menue":"H&ouml;llische Oberl&auml;nder Bratwurst mit Curry-Spezialsauce"},
    	           	{"name":"Wok", "price":2.45, "menue":"Paniertes Merlanfilet mit Kr&auml;uter-Dip und Zitrone"},
    	           	{"name":"Aktionstheke", "price":0.55, "menue":"Klare Rinderbr&uuml;he mit Nudeln"},
    	           	{"name":"Dessert", "price":0.30, "menue":"Vanillepudding"}
    	           ]	
    },
];